package com.example.sxy.servicedemo;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.sxy.service.SimpleService;


public class MainActivity extends Activity implements View.OnClickListener{
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;

    private SimpleService.MyBinder myBinder;
    //远程service
    private ServiceInterface aidlService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button) findViewById(R.id.button1);
        btn2 = (Button) findViewById(R.id.button2);
        btn3 = (Button) findViewById(R.id.button3);
        btn4 = (Button) findViewById(R.id.button4);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        Log.d("MainActivity", "process id is " +android.os.Process.myPid());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:
                Log.d("MyService","######Start Service######");
                Intent startIntent = new Intent(this, SimpleService.class);
                startService(startIntent);
                break;
            case R.id.button2:
                Log.d("MyService","######Stop Service######");
                Intent stopIntent = new Intent(this, SimpleService.class);
                stopService(stopIntent);
                break;
            case R.id.button3:
                Log.d("MyService","######Bind Service######");
                Intent bindIntent = new Intent(this, SimpleService.class);
//                BIND_AUTO_CREATE点击Bind Service按钮的时候Service会被创建
                bindService(bindIntent, connection, BIND_AUTO_CREATE);
                break;
            case R.id.button4:
                Log.d("MyService","######UnBind Service######");
                unbindService(connection);
                break;
            default:
                break;
        }
    }
//    服务连接监听，连接成功调用 myService中封装的方法
    private ServiceConnection connection = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
//            1.本地service的使用方法
//            myBinder = (SimpleService.MyBinder) service;
//            myBinder.startDownload();

//            2.跨进程通信
            aidlService = ServiceInterface.Stub.asInterface(service);
            try {
                int result = aidlService.plus(6, 16);
                String upperStr = aidlService.toUpperCase("hello world!");
                Log.d("MainActivity", "result is " + result);
                Log.d("MainActivity", "upperStr is " + upperStr);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    };
}
